<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>物资管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
            v-model="query.goodsName"
            placeholder="物品名称"
            class="handle-input mr10"
        ></el-input>
        <el-input
            v-model="query.goodsType"
            placeholder="物品类型"
            aria-placeholder="军训用品或者床上用品"
            class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
        >搜索
        </el-button
        >
        <el-button
            type="primary"
            icon="el-icon-circle-plus-outline"
            @click="handleAdd"
        >新增
        </el-button
        >
        <el-button ype="primary" @click="onReset">重置</el-button>
      </div>
      <div>
        <el-table
            :data="tableData"
            border
            class="table"
            ref="multipleTable"
            header-cell-class-name="table-header"
        >
          <el-table-column
              :show-overflow-tooltip="true"
              :prop="item.prop"
              :label="item.label"
              v-for="item in propt"
              :key="item.prop"
              align="center"
          >
          </el-table-column>
          <el-table-column label="物品样例" align="center">
            <template #default="scope">
              <p v-if="scope.row.goodsUrl!=null">
                       <span v-for="(item,index) in scope.row.goodsUrl.split(',')" :key="index">
                     <el-image style="display: inline-flex;padding: 10px;width: 30%;height: 10%"
                               class="table-td-thumb"
                               :src="item"
                               :preview-src-list="[item]"
                     ></el-image>
        </span>
              </p>

            </template>
          </el-table-column>
          <el-table-column>
            <el-table-column
                label="操作"
                width="180"
                align="center"
                fixed="right"
            >
              <template #default="scope">
                <el-button
                    type="text"
                    icon="el-icon-edit"
                    @click="handleEdit(scope.row.sid)"
                >编辑
                </el-button>
                <el-button
                    type="text"
                    icon="el-icon-delete"
                    class="red"
                    @click="handleDelete(scope.$index, scope.row.sid)"
                >删除
                </el-button
                >
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
              :currentPage="query.pageNum"
              :page-sizes="[10, 20, 40]"
              :page-size="query.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageTotal"
              @size-change="handleSizeChange"
              @current-change="handlePageChange"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {reactive, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {useRouter} from "vue-router";
import request from "../../utils/request";

export default {
  name: "Goods",
  setup() {
    const router = useRouter();
    const root = "/goods/";
    const propt = [
      {label: "物品名称", prop: "goodsName"},
      {label: "物品类型", prop: "goodsType"},
      {label: "创建时间", prop: "createTime"},
    ];
    // // 新增操作
    const handleAdd = () => {
      router.push("/admin/addGoods");
      getData();
    };
    const query = reactive({
      goodsName: "",
      goodsType: "",
      pageNum: 1,
      pageSize: 10,
    });
    const tableData = ref([]);
    const pageTotal = ref(0);
    // 获取表格数据
    const getData = () => {
      request
          .get("/goods/page", {
            params: query,
          })
          .then((res) => {
            tableData.value = res.data.records;
            pageTotal.value = res.data.total;
          });
    };
    // onBeforeMount(() => {
    getData();
    // });

    // 查询操作
    const handleSearch = () => {
      query.pageNum = 1;
      getData();
    };
    // 分页大小
    const handleSizeChange = (val) => {
      query.pageSize = val;
      getData();
    };
    // 分页导航
    const handlePageChange = (val) => {
      query.pageNum = val;
      getData();
    };
    const handleEdit = (sid) => {
      router.push({path: "/admin/addGoods", query: {id: sid}});
      getData();
    };
    const onReset = () => {
      query.goodsType = "",
          query.goodsName = ""
    };
    // 删除操作
    const handleDelete = (index, sid) => {
      // 二次确认删除
      ElMessageBox.confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
          .then(() => {
            request.delete(root + sid).then((res) => {
              if (res.data) {
                ElMessage.success("删除成功");
                tableData.value.splice(index, 1);
              } else {
                ElMessage.error("删除失败");
              }
            });
          })
          .catch(() => {
          });
    };


    return {
      propt,
      handleAdd,
      query,
      tableData,
      pageTotal,
      onReset,
      handleEdit,
      handleSearch,
      handleDelete,
      handleSizeChange,
      handlePageChange,

    }
  }
}
</script>

<style scoped>
.clist {
  background: #3ca2e0;
  height: 50px;
  font-size: 25px;
  display: flex;
  align-items: center;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>